//
//  DYTabBarViewController.m
//  DYTabBarViewController
//
//  Created by admin on 13-9-3.
//  Copyright (c) 2013年 cocoa. All rights reserved.
//

#import "DYTabBarViewController.h"
#import "DYTabBarButton.h"
#import "DYDiscoViewController.h"
#import "DYMusicViewController.h"
#import "DYCustomViewController.h"
#import "DYCameraViewController.h"
#import "DYColorController.h"
#import "DYInstallViewController.h"
#import "DYBaseNavigationViewController.h"

#define TabBarNum 6
#define TabBarSpace 10
#define TabBarW ((sWidth - TabBarSpace*2)/5)

@interface DYTabBarViewController ()
- (void)loadViewController;
- (void)loadSelfTabBarView;
@property (nonatomic, strong) NSMutableArray *navVcArray;
@property (nonatomic, weak) UIButton *selectBtn;
@end

@implementation DYTabBarViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.tabBar.hidden = YES;
        self.view.backgroundColor = [UIColor greenColor];
    }
    return self;
}


- (void)loadViewController{
    DYColorController* colorVc = [[DYColorController alloc] init];
    DYBaseNavigationViewController* colorNav = [[DYBaseNavigationViewController alloc] initWithRootViewController:colorVc];
    colorNav.TabbarImage = @"tab_color";
    colorNav.tabbarName = @"颜色";
    //[colorVc release];
    UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1];
    colorVc.tabBarItem = item;
    //[item release];
    
    
    DYInstallViewController * installVc = [[DYInstallViewController alloc] init];
    DYBaseNavigationViewController* installNav = [[DYBaseNavigationViewController alloc] initWithRootViewController:installVc];
    installNav.TabbarImage = @"tab_cct";
    installNav.tabbarName = @"内置模式";
    //[installVc release];
    UITabBarItem* item2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
    installVc.tabBarItem = item2;
    //[item2 release];
    
    
    DYMusicViewController *musicVc = [[DYMusicViewController alloc] init];
    DYBaseNavigationViewController* musicNav = [[DYBaseNavigationViewController alloc]initWithRootViewController:musicVc];
    musicNav.TabbarImage = @"tab_music";
    musicNav.tabbarName = @"音乐";
    //[musicVc release];
    UITabBarItem* item3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:3];
    musicVc.tabBarItem = item3;
    //[item3 release];
    
    
    DYCustomViewController *coustomVc = [[DYCustomViewController alloc] init];
    DYBaseNavigationViewController* coustomNav = [[DYBaseNavigationViewController alloc]initWithRootViewController:coustomVc];
    coustomNav.TabbarImage = @"tab_custom";
    coustomNav.tabbarName = @"自定义";
    //[coustomVc release];
    UITabBarItem* item4 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:4];
    coustomVc.tabBarItem = item4;
    //[item4 release];
    
    DYDiscoViewController *discoVc = [[DYDiscoViewController alloc] init];
    DYBaseNavigationViewController* discoNav = [[DYBaseNavigationViewController alloc]initWithRootViewController:discoVc];
    discoNav.TabbarImage = @"tab_mic";
    discoNav.tabbarName = @"DISCO";
    //[discoVc release];
    UITabBarItem* item5 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:5];
    discoVc.tabBarItem = item5;
    //[item5 release];
    
    
    DYCameraViewController * cameraVc = [[DYCameraViewController alloc] init];
    DYBaseNavigationViewController* cameraNav = [[DYBaseNavigationViewController alloc]initWithRootViewController:cameraVc];
    cameraNav.TabbarImage = @"tab_webcam";
    cameraNav.tabbarName = @"相机";
    //[cameraVc release];
    UITabBarItem* item6 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:6];
    cameraVc.tabBarItem = item6;
    //[item6 release];
    
    
    NSMutableArray* array = [NSMutableArray arrayWithObjects:colorNav,installNav,musicNav,coustomNav,discoNav ,cameraNav, nil];
    self.navVcArray = array;
    [self setViewControllers:array animated:YES];

    //[colorNav release];
    //[installNav release];
    //[musicNav release];
    //[coustomNav release];
    //[discoNav release];
    //[cameraNav release];
}

- (void)loadSelfTabBarView{
    
    //加载底部的bar的背景
    CGFloat tabBarBGY = sHeight - 49;
    tabBarBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, tabBarBGY, sWidth, 49)];
    tabBarBG.image = [UIImage imageNamed:@"title_background"];
    tabBarBG.userInteractionEnabled = YES;
    [self.view addSubview:tabBarBG];
    
    //增加按钮
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(TabBarSpace, 0, sWidth-2*TabBarSpace, 49)];
    scrollView.showsVerticalScrollIndicator = FALSE;
    scrollView.showsHorizontalScrollIndicator = FALSE;
    [tabBarBG addSubview:scrollView];
    [scrollView setContentSize:CGSizeMake(TabBarW*TabBarNum, 49)];
    
    UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(2, 5, 20, 20)];
    leftImageView.image = [UIImage imageNamed:@"img_tab_left"];
    [tabBarBG addSubview:leftImageView];
    
    UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake((sWidth-22), 5, 20, 20)];
    rightImageView.image = [UIImage imageNamed:@"img_tab_right"];
    [tabBarBG addSubview:rightImageView];
    
    for (int index = 0; index<TabBarNum; index++) {
        DYBaseNavigationViewController *nav = self.navVcArray[index];
        DYTabBarButton* button = [[DYTabBarButton alloc]init];
        button.tag = index;
        button.frame = CGRectMake((index*TabBarW), 49/2.0-20, TabBarW, 49);

        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_n",nav.TabbarImage]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_h",nav.TabbarImage]] forState:UIControlStateSelected];
        [button setTitleColor:DYColor(89, 160, 233) forState:UIControlStateSelected];
        if (index == 0) {
            button.selected = YES;
            self.selectBtn = button;
        }
        button.titleLabel.font = [UIFont systemFontOfSize:10];
        [button setTitle:nav.tabbarName forState:UIControlStateNormal];
        [scrollView addSubview:button];
        [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    //[scrollView release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadViewController];
    [self loadSelfTabBarView];
	// Do any additional setup after loading the view.
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showTabBar{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.34];
    tabBarBG.frame = CGRectMake(0, sHeight-49, sWidth, 49);
    [UIView commitAnimations];

}
- (void)hidenTabBar{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.35];
    tabBarBG.frame = CGRectMake(-sWidth, sHeight-49, sWidth, 49);
    [UIView commitAnimations];

}

- (void)changeViewController:(UIButton*)sender{

    if (self.selectedIndex == sender.tag) {
        return;
    }
    
    self.selectBtn.selected = !self.selectBtn.selected;
    sender.selected = !sender.selected;
    self.selectBtn = sender;
    self.selectedIndex = [sender tag];
    [UIView beginAnimations:nil context:NULL];
    [UIView commitAnimations];
}

@end
